//
//  HMComposeToolBar.swift
//  Black22
//
//  Created by Apple on 16/11/25.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

//  toolbar类型
enum HMComposeToolBarType: Int {
    //  图片
    case picture = 0
    //  @
    case mention = 1
    //  #
    case trend = 2
    //  表情
    case emoticon = 3
    //  加号
    case add = 4
    
}


class HMComposeToolBar: UIStackView {
    //  执行闭包回调
    var callBack: ((HMComposeToolBarType)->())?
    //  表情按钮
    var emoticonButton: UIButton?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        //  设置布局方式
        axis = .horizontal
        //  子控件填充方式
        distribution = .fillEqually
        
        addChildButton(imageName: "compose_camerabutton_background", type: .picture)
        addChildButton(imageName: "compose_mentionbutton_background", type: .mention)
        addChildButton(imageName: "compose_trendbutton_background", type: .trend)
        emoticonButton = addChildButton(imageName: "compose_emoticonbutton_background", type: .emoticon)
        addChildButton(imageName: "compose_add_background", type: .add)
    }
    
    //  添加子控件
    //  可以忽略返回值
    @discardableResult private func addChildButton(imageName: String, type: HMComposeToolBarType) -> UIButton {
        let button = UIButton()
        button.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
        //  枚举的原始值作为tag使用
        button.tag = type.rawValue
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
        //  去掉高亮效果
        button.adjustsImageWhenHighlighted = false
        
        //  添加子控件
        addArrangedSubview(button)
        return button
    }
    
    // MARK:    --点击事件
    @objc private func btnAction(btn: UIButton) {
        let type = HMComposeToolBarType(rawValue: btn.tag)!
        callBack?(type)
    }
    
    //  根据外界传入的是否是表情键盘类型，设置对应的icon
    func showIcon(isEmoticon: Bool) {
    
        if isEmoticon {
            //  如果是表情键盘，那么显示系统键盘icon图标
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), for: .normal)
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), for: .highlighted)
            
        } else {
            //  如果是系统键盘，那么显示表情键盘icon图标
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
        }
        
    }
    
 
}
